//
//  LGScreenOrderView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/14.
//  我的订单--筛选

#import "LGScreenOrderView.h"

#define itemHeight  viewPix(43)

@interface LGScreenOrderView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *typeTitle;
/**   */
@property (nonatomic , strong)UILabel *statusTitle;
/**   */
@property (nonatomic , assign)CGFloat typeBottom;

@end

@implementation LGScreenOrderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        self.typeArray = (!_typeArray || _typeArray.count == 0)?@[@{@"value":@"",@"title":@"全部"},@{@"value":@"1",@"title":@"线下复查"},@{@"value":@"2",@"title":@"视频咨询"},@{@"value":@"0",@"title":@"团队咨询"},@{@"value":@"3",@"title":@"专家咨询"}]:self.typeArray;
        self.typeArray = @[];
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.cancelBtn];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.sureBtn];
        [self.baseView addSubview:self.typeTitle];
        [self.baseView addSubview:self.statusTitle];
        [self performSelector:@selector(showViewAniamtion) withObject:nil afterDelay:0.2];
    }
    return self;
}

-(void)setTypeArray:(NSArray *)typeArray{
    _typeArray = typeArray;
}

-(void)setStatusArray:(NSArray *)statusArray{
    _statusArray = statusArray;
    [self createTypeView];
    UIButton *button = [self viewWithTag:1000+self.typeArray.count-1];
    CGFloat itemWidth = (Screen_W-viewPix(35)-viewPix(24))/4.0;
    CGFloat itemTop = viewPix(39);
    self.typeTitle.hidden = YES;
    if (self.typeArray.count>0) {
        self.typeTitle.hidden = NO;
//        itemTop = viewPix(102)+(itemHeight+viewPix(16))*(self.typeArray.count/3);
//        itemTop = (self.typeArray.count%3 == 0)?itemTop:itemTop+itemHeight;
        itemTop = self.typeBottom;
    }
    
    self.statusTitle.frame = CGRectMake(viewPix(22), itemTop+viewPix(30), viewPix(100), viewPix(21));
    for (NSInteger i=0; i<statusArray.count; i++) {
        NSDictionary *tempDic = statusArray[i];
        UIButton *button = [UIButton buttonWithTitle:tempDic[@"title"] titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(viewPix(20)+(itemWidth+viewPix(8))*(i%4), itemTop+viewPix(60)+(itemHeight+viewPix(16))*(i/4), itemWidth, itemHeight);
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        button.titleLabel.numberOfLines = 2;
        button.cornerRidus = viewPix(21);
        button.tag = 2000+i;
        [button addTarget:self action:@selector(statusButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        if([LGNSString(tempDic[@"value"]) isEqualToString:self.status]) button.selected = YES;
        if(i == statusArray.count-1){
            self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, kMaxY(button.frame)+viewPix(50));
        }
    }
}


-(void)cancelButtonAction{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)sureButtonAction{
    if(self.screenOrderResult){
        self.screenOrderResult(self.type, self.status);
    }
    [self cancelButtonAction];
}

//订单类型
-(void)typeButtonAction:(UIButton *)sender{
    for (NSInteger i=0; i<self.typeArray.count; i++) {
        NSDictionary *tempDic = self.typeArray[i];
        UIButton *button = [self viewWithTag:1000+i];
        if(sender.tag-1000 == i){
            self.type = LGNSString(tempDic[@"value"]);
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
}

//订单状态
-(void)statusButtonAction:(UIButton *)sender{
    for (NSInteger i=0; i<self.statusArray.count; i++) {
        NSDictionary *tempDic = self.statusArray[i];
        UIButton *button = [self viewWithTag:2000+i];
        if(sender.tag-2000 == i){
            self.status = LGNSString(tempDic[@"value"]);
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
}

-(void)showViewAniamtion{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, Screen_H-height+15, Screen_W, height);
    }];
}

-(void)createTypeView{
    self.typeBottom = viewPix(39);
    CGFloat itemWidth = (Screen_W-viewPix(50)-viewPix(24))/3.0;
    CGFloat itemTop = viewPix(102);
    CGFloat itemLeft = viewPix(20);
    for (NSInteger i=0; i<self.typeArray.count; i++) {
        NSDictionary *tempDic = self.typeArray[i];
        NSString *content = LGNSString(tempDic[@"title"]);
        CGFloat contentW = [content textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:itemHeight]+viewPix(40);
        contentW = contentW<itemWidth?itemWidth:contentW;
        itemTop = (itemLeft+contentW>Screen_W-viewPix(40))?itemTop+itemHeight+viewPix(16):itemTop;
        itemLeft = (itemLeft+contentW>Screen_W-viewPix(40))?viewPix(20):itemLeft;
        UIButton *button = [UIButton buttonWithTitle:tempDic[@"title"] titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(itemLeft, itemTop, contentW, itemHeight);
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        button.cornerRidus = viewPix(21);
        button.tag = 1000+i;
        [button addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        if([LGNSString(tempDic[@"value"]) isEqualToString:self.type]) button.selected = YES;
        itemLeft += contentW + viewPix(12);
        self.typeBottom = kMaxY(button.frame);
    }
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(350))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(14, UIFontWeightSemibold) textColor:@"#999999" imageName:@""];
        _cancelBtn.frame = CGRectMake(viewPix(15), viewPix(15), viewPix(48), viewPix(30));
        [_cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" titleFont:LGFontWeight(14, UIFontWeightSemibold) textColor:@"#429BFF" imageName:@""];
        _sureBtn.frame = CGRectMake(Screen_W-viewPix(63), viewPix(15), viewPix(48), viewPix(30));
        [_sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"订单筛选" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(100), viewPix(15), Screen_W-viewPix(200), viewPix(30));
    }
    return _titleLabel;
}

-(UILabel *)typeTitle{
    if(!_typeTitle){
        _typeTitle = [UILabel lableWithText:@"订单类型" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _typeTitle.frame = CGRectMake(viewPix(22), viewPix(69), viewPix(100), viewPix(21));
    }
    return _typeTitle;
}


-(UILabel *)statusTitle{
    if(!_statusTitle){
        _statusTitle = [UILabel lableWithText:@"订单状态" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _statusTitle.frame = CGRectMake(viewPix(22), viewPix(142)+kMaxY(self.typeTitle.frame), viewPix(100), viewPix(21));
    }
    return _statusTitle;
}
    

@end
